import streamlit as st


def launch_task_result(task_name, *args, **kwargs):
    tmp = launch_task(task_name, *args, **kwargs)
    success, pid, msg = tmp
    if success:
        st.success(
            f"Task {task_name} started successfully!\n"
            f"Id: {pid}\n"
            f"Args: {args}, kwargs: {kwargs}"
            f"Message: {msg}"
        )
    else:
        st.error(
            f"Task {task_name} failed to start!\n" f"Args: {args}, kwargs: {kwargs}"
        )


def draw_form_params(params: list):
    """
    这个函数用streamlit生成一个根据参数信息列表，生成一个前端参数输入表单，点击提交后，
    后端通过输入的参数调用指定函数，并向前端返回指定结果，params中每一项类似于
    {"name": "param1", "type": "int", "description": "Parameter 1 description", "default": 1},
    """
    input_values = {}

    for param in params:
        if param["type"] == "int":
            input_values[param["name"]] = st.number_input(
                param["name"],
                value=param.get("default", 0),
                step=1,
                help=param.get("description"),
                min_value=param.get("min"),
                max_value=param.get("max"),
            )
        elif param["type"] == "float":
            input_values[param["name"]] = st.number_input(
                param["name"],
                value=param.get("default", 0.0),
                step=param.get("step", 0.01),
                help=param.get("description"),
                min_value=param.get("min"),
                max_value=param.get("max"),
            )
        elif param["type"] in ("str", "string"):
            input_values[param["name"]] = st.text_input(
                param["name"],
                value=param.get("default", ""),
                key=param["name"],
                help=param.get("description"),
            )
        elif param["type"] == "date":
            input_values[param["name"]] = st.date_input(
                param["name"],
                value=param.get("default"),
                key=param["name"],
                help=param.get("description"),
            )
        elif param["type"] == "bool":
            input_values[param["name"]] = st.checkbox(
                param["name"],
                value=param.get("default"),
                key=param["name"],
                help=param.get("description"),
            )
        else:
            st.warning(
                f"Unsupported parameter type: {param['type']} for {param['name']}"
            )

    return input_values
